package com.bili.bili_data_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bili.bili_data_api.dao.mapper.GuochanMapper;
import com.bili.bili_data_api.dao.pojo.Guochan;
import com.bili.bili_data_api.dao.pojo.bangumi;
import com.bili.bili_data_api.service.GuoChanService;
import com.bili.bili_data_api.util.Result;
import com.bili.bili_data_api.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GuoChanServiceImpl implements GuoChanService {
    @Autowired
    private GuochanMapper guochanMapper;

    @Override
    public GuoChanVo getAll(QueryVo queryVo) {
        Page<Guochan> page = new Page<>(queryVo.getPageIndex(),queryVo.getPageSize());
        Page<Guochan> guochanPage = guochanMapper.selectPage(page, null);
        Integer integer = guochanMapper.selectCount(null);
//        List<Guochan> guochans = guochanMapper.selectList(null);
        GuoChanVo guoChanVo = new GuoChanVo();
        List<GuoChanList> guoChanLists = new ArrayList<>();
        for (Guochan guochan : guochanPage.getRecords()) {
            GuoChanList guoChanList = new GuoChanList();
            guoChanList.setId(guochan.getId());
            guoChanList.setComment(guochan.getComment());
            guoChanList.setFan(guochan.getFan());
            guoChanList.setScore(guochan.getScore());
            guoChanList.setTitle(guochan.getTitle());
            guoChanList.setViews(guochan.getViews());
            guoChanLists.add(guoChanList);
        }
        guoChanVo.setGuoChanLists(guoChanLists);
        guoChanVo.setTotal(integer);
        return guoChanVo;
    }

    @Override
    public Result edit(GuoChanList guoChanVo) {
        Guochan guochan = new Guochan();
        guochan.setId(guoChanVo.getId());
        guochan.setComment(guoChanVo.getComment());
        guochan.setFan(guoChanVo.getFan());
        guochan.setScore(guoChanVo.getScore());
        guochan.setTitle(guoChanVo.getTitle());
        guochan.setViews(guoChanVo.getViews());
        guochanMapper.updateById(guochan);
        return Result.ok();
    }

    @Override
    public Result delete(GuoChanList guoChanVo) {
        guochanMapper.deleteById(guoChanVo.getId());
        return Result.ok();
    }

    @Override
    public Result add(Guochan guochan) {
        guochanMapper.insert(guochan);
        return Result.ok();
    }

    @Override
    public GuoChanVo selectByTitle(QueryVo queryVo) {
        GuoChanVo guoChanVo = new GuoChanVo();
        Page<Guochan> page = new Page<>(queryVo.getPageIndex(),queryVo.getPageSize());
        LambdaQueryWrapper<Guochan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Guochan::getTitle,queryVo.getTitle());
//        List<Guochan> guochans = guochanMapper.selectList(lambdaQueryWrapper);
        Page<Guochan> guochanPage = guochanMapper.selectPage(page, lambdaQueryWrapper);
        Integer integer = guochanMapper.selectCount(lambdaQueryWrapper);
        List<GuoChanList> guoChanLists = new ArrayList<>();
        for (Guochan guochan : guochanPage.getRecords()) {
            GuoChanList guoChanList = new GuoChanList();
            guoChanList.setId(guochan.getId());
            guoChanList.setTitle(guochan.getTitle());
            guoChanList.setViews(guochan.getViews());
            guoChanList.setComment(guochan.getComment());
            guoChanList.setFan(guochan.getFan());
            guoChanList.setScore(guochan.getScore());
            guoChanLists.add(guoChanList);
        }
        guoChanVo.setGuoChanLists(guoChanLists);
        guoChanVo.setTotal(integer);
        return guoChanVo;
    }
}
